
var model = require("./user_model");
var fs = require('fs');
var json2csv = require('json2csv');


model.find( (err, docu) => {
    console.log(docu.length);

    var students = [];
    for (var i = 0; i <= docu.length; i++){
        students.push(docu[i])
    }

    var fields = ['name1',
        'number1',
        'mark1',
        'name2',
        'number2',
        'mark2',
        'name3',
        'number3',
        'mark3',
        'name4',
        'number4',
        'mark4',
        'group',
        'teacher'];

    var csv = json2csv({ data: students, fields: fields });

    fs.writeFile('grades.csv', csv, function(err) {
        if (err) throw err;
        console.log(docu.length);
        console.log('\nfile saved');
    });
});
